<?php

namespace app\web\controller;

use app\user\model\UserModel;
use app\web\model\WebModel;
use app\web\model\WebTestModel;
use app\web\model\WebExchangeModel;
use cmf\controller\AdminBaseController;
use think\Request;

class WebController extends AdminBaseController
{
    public function webNumber()
    {
        $num = 0;
        foreach (scandir(HTML_PATH) as $index => $item) {
            if (is_dir(HTML_PATH . $item) && substr($item, 0, 4) == 'www.') {
                $num += 1;
            }
        }
        return $num;
    }

    public function webList()
    {
        $list = (new WebModel())->order('updated_at', 'desc')->paginate(20);
        $this->assign('list', $list);
        $this->assign('page', $list->render());
        return $this->fetch();
    }

    public function webTestList()
    {
        if ($this->request->isPost() && ! empty($serverName = $this->request->post('server_name'))) {
            $list = (new WebTestModel())->where(['server_name' => $serverName])->select();
            $this->assign('list', $list);
            return $this->fetch();
        }

        $list = (new WebTestModel())->order('id', 'desc')->paginate(20);
        $this->assign('list', $list);
        $this->assign('page', $list->render());
        $this->assign('webNumber', $this->webNumber());
        return $this->fetch();
    }

    public function webExchangeList()
    {
        $list = (new WebExchangeModel())->order('updated_at', 'desc')->paginate(20);
        $this->assign('list', $list);
        $this->assign('page', $list->render());
        return $this->fetch();
    }

    public function saveWebStatus()
    {
        $webTest = WebTestModel::get($this->request->get('id'));
        $webTest->saveStatus();
        $this->redirect(url('web/webTestList'), 0);
    }

    public function replicable()
    {
        $webTest = WebTestModel::get($this->request->get('id'));
        $webTest->saveCopyStatusToReplicable();
        $this->redirect(url('web/webTestList'), 0);
    }

    public function noReplicable()
    {
        $webTest = WebTestModel::get($this->request->get('id'));
        $webTest->saveCopyStatusToNoReplicable();
        $this->redirect(url('web/webTestList'), 0);
    }

    // 添加测试站点
    public function add()
    {
        if ($this->request->isPost()) {
            if (! $projectName = $this->request->param('project_name')) {
                $this->error('请输入必填项');
            }
            exec('sh /data/configwebsite.sh ' . $projectName);

            $admin = (new UserModel())->get(cmf_get_current_admin_id())['user_login'];
            if (! $web = WebTestModel::get(['server_name' => $projectName])) {
                (new WebTestModel())->add($projectName, $admin);
            }
            else {
                $web->data([
                    'admin_name' => $admin,
                    'updated_at' => date('Y-m-d H:i:s')
                ])->save();
            }
            $this->success('', url('web/webTestList'));
        }
        return $this->fetch();
    }

    // 复制站点
    public function copy()
    {
        if ($this->request->isPost()) {
            $projectName = $this->request->param('project_name');
            $oldServerName = $this->request->param('old_server_name');
            if (empty($projectName) || empty($oldServerName)) {
                $this->error('请输入必填项');
            }
            if (! $web = WebTestModel::get(['server_name' => $oldServerName])) {
                $this->error('无效的测试站域名');
            }

            system('sh /data/overwiertdir.sh ' . $oldServerName . ' ' . $projectName);

            $admin = (new UserModel())->get(cmf_get_current_admin_id())['user_login'];
            if (! $web = WebTestModel::get(['server_name' => $projectName])) {
                (new WebTestModel())->add($projectName, $admin);
            }
            else {
                $web->data([
                    'admin_name' => $admin,
                    'updated_at' => date('Y-m-d H:i:s')
                ])->save();
            }
            $this->redirect(url('web/webTestList'), 0);
        }
        return $this->fetch();
    }

    // 转移站点
    public function exchange()
    {
        if ($this->request->isPost()) {
            $newServerName = $this->request->param('new_server_name');
            $oldServerName = $this->request->param('old_server_name');
            if (empty($newServerName) || empty($oldServerName)) {
                $this->error('请输入必填项');
            }
            if (! $web = WebTestModel::get(['server_name' => $oldServerName])) {
                $this->error('无效的测试站域名');
            }

            //exec('sh /data/renamewebsite/run.sh ' . $oldServerName . ' ' . $newServerName);
            system('sh /data/renamewebsite/run.sh ' . $oldServerName . ' ' . $newServerName);

            $admin = (new UserModel())->get(cmf_get_current_admin_id())['user_login'];
            (new WebExchangeModel())->add($newServerName, $oldServerName, $admin);
            $web->saveWeb($newServerName, $admin);

            $this->redirect(url('web/webExchangeList'), 0);
        }
        return $this->fetch();
    }

    // 正式站部署
    public function deploy()
    {
        if ($this->request->isPost()) {
            $serverIp      = $this->request->param('server_ip');
            $newServerName = $this->request->param('new_server_name');
            $oldServerName = $this->request->param('old_server_name');
            if (empty($serverIp) || empty($newServerName) || empty($oldServerName)) {
                $this->error('请输入必填项');
            }
            if (! $webTest = WebTestModel::get(['server_name' => $oldServerName])) {
                $this->error('无效的测试站域名');
            }

            $web = new WebModel();
            $admin = (new UserModel())->get(cmf_get_current_admin_id())['user_login'];
            $web->add($serverIp, $newServerName, $oldServerName, $admin);

            exec('sh /data/copywebsite/initshell.sh ' . $serverIp . ' ' . $oldServerName . ' ' . $newServerName . ' ' . $web->id);
            $webTest->saveStatus();
            $this->redirect(url('web/webList'), 0);
        }

        return $this->fetch();
    }

    public function fileReplacementTest()
    {
        if ($this->request->isPost()) {
            $path = $this->request->param('path');
            $file = $this->request->param('file');

            $dir = scandir(HTML_PATH);

	        foreach ($dir as $index => $item) {
                if (is_dir(HTML_PATH . $item) && substr($item, 0, 4) == 'www.') {
                    $filePath = HTML_PATH . $item . $path;
                    $copyName = HTML_PATH . $item . $path . date('YmdHis') . '.copy';
                    @rename($filePath, $copyName);
                    copy(HTML_PATH . 'website/public/upload/' . $file, HTML_PATH . $item . $path);
                }
            }

            $this->success('替换完成');
        }
        return $this->fetch();
    }

    public function fileReplacement()
    {
        if ($this->request->isPost()) {
            $path = $this->request->param('path');
            $file = $this->request->param('file');
            $info = $this->request->param('info');

            if (empty($path) || empty($file) || empty($info)) {
                $this->error('请输入必填项');
            }

            exec('sh /data/remoteoverwrite/overwiertdir.sh ' . HTML_PATH . 'website/public/upload/' . $file .
                ' ' . $info . ' ' . $path);

            $this->success('替换完成');
        }
        return $this->fetch();
    }

    // 脚本回调
    public function callback()
    {
        $webId = $this->request->get('web_id');
        $web = WebModel::get($webId);
        $web->status = WebModel::STATUS_SUCCESSED;
        return $web->save();
    }
}
